# -*- coding: utf-8 -*-
import scrapy
import re
from scrapy.http import Request
from scrapy.http import JsonRequest
from pictures.items import picturespiderItem
import json

import logging
logger = logging.getLogger(__name__)

class picapispider(scrapy.Spider):

    name = "picapispider"
    cover = "picapispider"
    title = "picapispider"
    allowed_domains = ["m.yituyu.com","api.yituyu.com"]
    start_urls = ["https://m.yituyu.com/"]

    def start_requests(self):
        yield Request(url=f"https://m.yituyu.com")

    def parse(self, response):
        #   解析标签

        label_list = response.xpath("//div[@class='tags']/a")

        for label in label_list:
            valueLabel = label.xpath("@href").extract_first()
            name = label.xpath("./text()").extract_first()

            for page in range(1, 11):
                # /tag/180/p1/
                yield Request(url=f"https://m.yituyu.com{valueLabel}p{page}", callback=self.parse_info,meta={"title":name})
        # yield Request(url=f"https://m.yituyu.com/tag/180/p1/", callback=self.parse_info)
        
        # cover = f"https://img.yituyu.com/gallery/7085/cover.jpg"
        # id="7085"
        # title="测试"
        # # 通过h5端拿所有分类
        # # 再通过app 接口去爬详情全部数据
        # # https://api.yituyu.com/index.php?c=Gallery&a=detail&SYS=APP
        # token="sLm5+BYHuJpXpInEKTjOoC/O0OCPUDudEINKjRFKQgwHeXNRHKn9R4MV4ZH6ZnPsDdp+x6oRjQLGCfoEyIxYlBcGcr9LibF/cx3mveCmS1k="
        # yield Request(url=f"https://api.yituyu.com/index.php?c=Gallery&a=detail&SYS=APP",
        #               method="POST",
        #               body=json.dumps({"id":id}),
        #               headers={
        #                   "content-type": "application/json",
        #                   'token':token
        #                    },
        #               callback=self.parse_info3,
        #               meta={"cover":cover,"title":title})

    
    def parse_info(self, response):
        title=response.meta["title"]
        li_list = response.xpath("//div[@class='box']/ul/li")
        token="sLm5+BYHuJpXpInEKTjOoC/O0OCPUDudEINKjRFKQgwHeXNRHKn9R4MV4ZH6ZnPsDdp+x6oRjQLGCfoEyIxYlBcGcr9LibF/cx3mveCmS1k="
        for li in li_list:
             data_url = li.xpath("div[@class='img-box']/a/@href").extract_first()
             cover = f"https://img.yituyu.com{data_url}cover.jpg"
             # 7085
            #  self.parse_api(self,data_url.replace("/gallery/",''),cover,title)
             yield Request(url=f"https://api.yituyu.com/index.php?c=Gallery&a=detail&SYS=APP",
                      method="POST",
                      body=json.dumps({"id":data_url.replace("/gallery/",'')}),
                      headers={
                          "content-type": "application/json",
                          'token':token
                           },
                      callback=self.parse_info3,
                      meta={"cover":cover,"title":title})
    
   
       # 接口爬取
    def parse_info3(self, response):
        
        dict_obj =response.json() 
        item = picturespiderItem()
        data = dict_obj['data']
        # 为了兼容h5匹配名字，接口没有 模拟一个填充
        imgNames =[f"《{data['title']}》" for n in range(len(data['imgs']))]
        item['images']=data['imgs']
        item['type']="pic"
        item['image_names']=imgNames
        item['cover']= response.meta["cover"]
        item['title']=response.meta["title"]
       
        yield item
   
       